<template>
  <div class="register">
    <!-- 头部模块 -->
    <Lheader></Lheader>

    <!-- 注册模块 -->
    <div class="module">
      <div class="logo">
        <h1>HarSine</h1>
      </div>

      <div class="title">
        <h3>注册账户</h3>
      </div>

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
        <el-form-item prop="email">
          <el-input style="width: 330px" v-model="ruleForm.email" placeholder="请输入邮箱账号"></el-input>
        </el-form-item>
        <el-form-item prop="authCode">
          <el-input style="width: 330px" v-model="ruleForm.authCode" placeholder="请输入验证码">
            <el-button style="background-color: #f63f00; color: white" slot="append" icon="el-icon-search" @click="getAuthCode()">获取验证码</el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码（6-20位数字、字母组合）"></el-input>
        </el-form-item>
        <el-form-item prop="checkPassword">
          <el-input type="password" v-model="ruleForm.checkPassword" placeholder="再次输入密码（6-20位数字、字母组合）"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style=" width: 330px; background-color: #f63f00; border-color: white !important;" type="primary" @click="submitForm('ruleForm')">注册</el-button>
        </el-form-item>
      </el-form>

      <div class="bottom">
        <p>
          请仔细阅读红信
          <a href="" style="color: #6d9ffd; text-decoration: none">服务协议、隐私政策</a>
          ，登录、注册即表示同意
        </p>
        <router-link to="/Login" style="color: #6d9ffd; text-decoration: none">已有账号，去登录 ></router-link>
      </div>
    </div>

    <!-- 脚部模块 -->
    <Lfooter></Lfooter>
  </div>
</template>

<script>
import { register, getAuthCode } from "@/api/login";

export default {
  data() {
    //验证邮箱账号格式
    var checkEmail = (rule, value, callback) => {
      const ruless = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      if (!ruless.test(value)) {
        return callback(new Error("邮箱格式错误"));
      } else {
        callback();
      }
    };
    //验证密码格式
    var checkPassword = (rule, value, callback) => {
      const CPassword = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{6,20}$/;
      if (!CPassword.test(value)) {
        return callback(new Error("密码格式错误"));
      } else {
        callback();
      }
    };
    //二次验证密码
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        email: "1100998293@qq.com",
        authCode: "",
        password: "zsl010716",
        checkPassword: "zsl010716",
        delivery: false,
      },
      rules: {
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
          {
            validator: checkEmail,
            trigger: "blur",
          },
        ],
        authCode: [
          {
            required: true,
            message: "请输入验证码",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            validator: checkPassword,
            trigger: "blur",
          },
        ],
        checkPassword: [
          {
            required: true,
            validator: validatePass2,
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //提交注册
    submitForm(formName) {
      //验证所有的内容（是否填了，且是否格式正确），都正确就发请求
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log("验证通过，去注册");

          //注册请求
          register({
            //请求参数
            email: this.ruleForm.email,
            code: this.ruleForm.authCode,
            password: this.ruleForm.password,
            re_password: this.ruleForm.checkPassword,
          }).then((res) => {
            console.log(res);
          });

          //注册成功后跳转到登录页面
          this.$router.push({
            path: "/Login",
          });
        } else {
          console.log("格式错误");
          return false;
        }
      });
    },
    //获取验证码
    getAuthCode() {
      //根据表单的ref属性取到整个表单的值，查看有没有email，格式是否正确
      this.$refs["ruleForm"].validateField("email", (valid) => {
        if (!valid) {
          console.log("有邮箱，去发验证码");

          //发验证码
          getAuthCode({
            email: this.ruleForm.email,
          }).then((res) => {
            console.log(res);
          });
        } else {
          console.log("没有邮箱，请输入");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.register {
  height: 100vh;

  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}
/* 注册模块 */
.module {
  margin: 15vh 0;
  padding: 25px;
  border-radius: 5%;
  box-shadow: 0 6px 10px 10px #f7f7f7;
}
.logo {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 20px;
}

.title {
  margin: 23px 0;
}

.bottom {
  width: 330px;

  display: flex;
  flex-direction: column;
  align-items: center;
}
.bottom p {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 10px;
  margin: -10px 0 20px 0;
  color: #bec4ca;
}
</style>